package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.RoleMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Service
@RequestMapping("/relo")
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void deleteByPrimaryKey(Long id) {
        roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(Role role,Long[] ids) {
        roleMapper.insert(role);
        if (ids != null){
            for (Long id : ids) {
                roleMapper.insertPermissionAndRole(role.getId(),id);
            }
        }

    }

    @Override
    public Role selectByPrimaryKey(Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Role> selectAll() {
        return roleMapper.selectAll();
    }

    @Override
    public void updateByPrimaryKey(Role role,Long[] ids) {
        roleMapper.deleteByIds(role.getId());
        roleMapper.updateByPrimaryKey(role);
        if (ids != null){
            for (Long id : ids) {
                roleMapper.insertPermissionAndRole(role.getId(), id);
            }
        }
    }

    @Override
    public PageInfo<Role> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Role> query = roleMapper.query(qo);
        return new PageInfo<Role>(query);
    }
}
